import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'
import store from '@/store'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/home',
    name: 'home',
    component: HomeView
  },
  {
    path: '/goods',
    name: 'goods',
    component: () => import(/* webpackChunkName: "about" */ '../views/Goods.vue')
  },
  {
    path: '/free',
    name: 'free',
    component: () => import(/* webpackChunkName: "about" */ '../views/Free.vue')
  },
  {
    path: '/user',
    name: 'user',
    component: () => import(/* webpackChunkName: "about" */ '../views/User.vue'),
    redirect: '/user/cart',
    children: [
      {
        path: 'cart',
        component: () => import(/* webpackChunkName: "about" */ '../components/user/Cart.vue'),
      }
    ]

    // beforeEnter: (to, from, next) => {
    //   // reject the navigation
    // let token = localStorage.getItem('x-auth-token')
    // if (token) {
    //   next()
    // }
    // else {
    //   store.dispatch('showToast/asyncChangeIsShowLoginToast', {
    //     isShow: true,
    //     Msg: "请先登录",
    //     Type: "danger",
    //   })
    // }
    //   return
    // },
  },
  {
    path: '/order',
    name: 'order',
    component: () => import(/* webpackChunkName: "about" */ '../views/Order.vue')
  },
  {
    path: '/details',
    name: 'details',
    component: () => import(/* webpackChunkName: "about" */ '../views/Details.vue')
  },
  {
    path: '*',
    name: 'error',
    component: () => import(/* webpackChunkName: "about" */ '../views/error.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// router.beforeEach((to, from, next) => {
//   if (to.path == '/user') {
//     let token = localStorage.getItem('x-auth-token')
//     if (token) {
//       next()
//     }
//     else {
//       store.dispatch('showToast/asyncChangeIsShowLoginToast', {
//         isShow: true,
//         Msg: "请先登录",
//         Type: "danger",
//       })
//     }
//     return
//   }
//   next();

// })

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}


export default router
